<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Figure 6.19: Polynomial fitting</title>
<link rel="canonical" href="/Users/mcgrant/Repos/CVX/examples/cvxbook/Ch06_approx_fitting/html/fig6_19.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Figure 6.19: Polynomial fitting</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 6.5.3</span>
<span class="comment">% Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Original by Lieven Vandenberghe</span>
<span class="comment">% Adapted for CVX by Joelle Skaf - 10/03/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% Given data u_1,...,u_m and v_1,...,v_m in R, the goal is to fit to the</span>
<span class="comment">% data a polynomial of the form</span>
<span class="comment">% p(u) = x_1 + x_2*u + ... + x_n*u^{n-1}</span>
<span class="comment">% i.e. solve the problem:  minimize ||Ax - v||</span>
<span class="comment">% where A is the Vandermonde matrix s.t. Aij = u_i^{j-1}</span>
<span class="comment">% Two cases are considered: L2-norm and Linfty-norm</span>

<span class="comment">% Input data</span>
n=6;
m=40;
randn(<span class="string">'state'</span>,0);
<span class="comment">% generate 50 ponts ui, vi</span>
u = linspace(-1,1,m);
v = 1./(5+40*u.^2) + 0.1*u.^3 + 0.01*randn(1,m);


<span class="comment">% LS fit polynomial x_1 + x_2*u + ... + x_n*u^(n-1) to (ui,vi)</span>
fprintf(1,<span class="string">'Computing optimal polynomial in the case of L2-norm...'</span>);

A = vander(u');
A = A(:,m-n+[1:n]);     <span class="comment">% last n columns of A</span>
x = A\(v');             <span class="comment">% coefficients of the polynomial in the following</span>
                        <span class="comment">% order: x = [x_n x_(n-1) ... x_2 x_1]'</span>

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% L-infty fit</span>
fprintf(1,<span class="string">'Computing optimal polynomial in the case of Linfty-norm...'</span>);

cvx_begin <span class="string">quiet</span>
    variable <span class="string">x1(n)</span>
    minimize (norm(A*x1 - v', inf))
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% generates 1000 points in  [-1,1]</span>
u2 = linspace(-1.1,1.1,1000);

<span class="comment">% evaluate the interpolating polynomial using Horner's method</span>
vpol = x(1)*ones(1,1000);
vpoll1 = x1(1)*ones(1,1000);
<span class="keyword">for</span> i = 2:n
  vpol = vpol.*u2 + x(i);
  vpoll1 = vpoll1.*u2 + x1(i);
<span class="keyword">end</span>;

figure
<span class="comment">% plot function and interpolating polynomial</span>
plot(u2, vpol,<span class="string">'-'</span>, u, v, <span class="string">'o'</span>, u2, vpoll1,<span class="string">'--'</span>);
xlabel(<span class="string">'u'</span>);
ylabel(<span class="string">'p(u)'</span>);
title(<span class="string">'Fitting of data points with two polynomials of degree 5'</span>);
legend(<span class="string">'L_2 norm'</span>,<span class="string">'data points'</span>,<span class="string">'L_{\infty} norm'</span>, <span class="string">'Location'</span>,<span class="string">'Best'</span>);
<span class="comment">% print -deps polapprox.eps</span>
</pre>
<a id="output"></a>
<pre class="codeoutput">
Computing optimal polynomial in the case of L2-norm...Done! 
Computing optimal polynomial in the case of Linfty-norm...Done! 
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="fig6_19__01.png" alt=""> 
</div>
</div>
</body>
</html>